package gui;

import comm.OutputTask;

import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: dna
 * Date: 27.04.13
 * Time: 15:01
 */
public class AddCityPopUp extends JFrame {

    private JTextField fieldByName;
    private JTextField fieldByIp;
    private JTextField fieldByCoordinateX;
    private JTextField fieldByCoordinateY;

    private JLabel outputLabel;
    private OutputTask outputTask;

    public AddCityPopUp(JLabel output, OutputTask outputTask) throws HeadlessException {

        this.outputTask = outputTask;
        this.outputLabel = output;

        setTitle("Add new City");

        JLabel labelByName = new JLabel("City name:");
        JLabel labelByIp = new JLabel("City IP:");
        JLabel labelByCoordinate = new JLabel("City Coordinates:");

        JButton addByName = new JButton("Add");
        JButton addByIp = new JButton("Add");
        JButton addByCoordinate = new JButton("Add");


        addByName.addActionListener(new AddByName());
        addByIp.addActionListener(new AddByIP());
        addByCoordinate.addActionListener(new AddByCoordinates());

        fieldByName = new JTextField();
        fieldByIp = new JTextField();
        fieldByCoordinateX = new JTextField();
        fieldByCoordinateY = new JTextField();

        JPanel fieldByCoordinate = new JPanel(new GridLayout(2, 1));
        fieldByCoordinate.add(fieldByCoordinateX);
        fieldByCoordinate.add(fieldByCoordinateY);

        GridLayout layout = new GridLayout(3, 3);

        getContentPane().setLayout(layout);

        getContentPane().add(labelByName);
        getContentPane().add(fieldByName);
        getContentPane().add(addByName);

        getContentPane().add(labelByIp);
        getContentPane().add(fieldByIp);
        getContentPane().add(addByIp);

        getContentPane().add(labelByCoordinate);
        getContentPane().add(fieldByCoordinate);
        getContentPane().add(addByCoordinate);

        pack();
    }

    private class AddByName implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            String name = fieldByName.getText();
            try {
                outputTask.addCityByName(fieldByName.getText());
                outputLabel.setText("Added city by name: " + name);
            } catch (IOException e1) {
                e1.printStackTrace();
                outputLabel.setText("<html><span style=\"color:red\">Error adding city " + name + "</span></html>");
            } catch (ParserConfigurationException e1) {
                e1.printStackTrace();
            } catch (TransformerException e1) {
                e1.printStackTrace();
            }

            dispose();
        }
    }

    private class AddByIP implements ActionListener{

        private static final String PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +  "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

        public boolean validate(final String ip){

            Pattern pattern = Pattern.compile(PATTERN);
            Matcher matcher = pattern.matcher(ip);
            return matcher.matches();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String ip = fieldByIp.getText();
            if (validate(ip)){
                try {
                    outputTask.addCityByIp(ip);
                    outputLabel.setText("Added city by IP: " + ip);
                } catch (IOException e1) {
                    e1.printStackTrace();
                    outputLabel.setText("<html><span style=\"color:red\">Error adding by ip " + ip + "</span></html>");
                } catch (ParserConfigurationException e1) {
                    e1.printStackTrace();
                } catch (TransformerException e1) {
                    e1.printStackTrace();
                }
            } else {
                outputLabel.setText("<html><span style=\"color:red\">Invalid IP: " + ip + "</span></html>");
            }
            dispose();
        }
    }

    private class AddByCoordinates implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            String latitude = fieldByCoordinateX.getText();
            String longitude = fieldByCoordinateY.getText();

            try {
                outputTask.addCityByCoord(latitude,longitude);
                outputLabel.setText("Added city by coordinates: " + latitude + " " + longitude);
            } catch (IOException e1) {
                outputLabel.setText("<html><span style=\"color:red\">Error adding by coordinates " + latitude + " " + longitude + "</span></html>");
            } catch (ParserConfigurationException e1) {
                outputLabel.setText("<html><span style=\"color:red\">Error adding by coordinates " + latitude + " " + longitude + "</span></html>");
            } catch (TransformerException e1) {
                outputLabel.setText("<html><span style=\"color:red\">Error adding by coordinates " + latitude + " " + longitude + "</span></html>");
            }
            dispose();
        }
    }
}
